var express = require('express');
var router = express.Router();
var multer = require('multer');
var fs = require('fs');
var upload = multer({
    dest: "upload"
})
const single = upload.single("myfile");

const arr = [];

/* GET home page. */
router.get('/', function(req, res, next) {
    res.locals.user = req.session.user||"";
    res.locals.message = req.session.message||"";
    res.locals.list = arr;
    res.render('index');
});

//显示图片
router.get('/img/:imgName', function(req, res){
    let path = "upload/"+req.params.imgName;
    let rs = fs.createReadStream(path);
    rs.pipe(res);
})

//上传
router.post('/upload', single, function(req, res){
    arr.push(req.file);
    res.send(req.file);
})


//删除
// router.post('/del', function(req, res){
//     var delfile = "upload/"+req.body.delName;
//     fs.unlinkSync(delfile , (err)=>{
//         console.log(err);
//     })
//
//     for(index in arr){
//         if(arr[index].filename==req.body.delName)
//         {
//             arr.splice(index, 1);
//         }
//     }
//
//     res.redirect("back");
// })

module.exports = router;
